package com.nt.mapreduce020201_hello.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.util.Arrays;

public class Hello01HDfs {

    public static void main(String[] args) throws IOException {
//        new Hello01HDfs().pathInfo();
        new Hello01HDfs().upload_download();
    }

    /**
     * 文件信息
     *
     * @throws IOException
     */
    private static void pathInfo() throws IOException {
//加载配置文件
        Configuration configuration = new Configuration(true);
//获取文件系统
        FileSystem fileSystem = FileSystem.get(configuration);
//创建路径的Path对象
        Path path = new Path("/lzj/harry.txt");
        boolean flag = fileSystem.exists(path);
        if (flag) {
            System.out.println(fileSystem.getDefaultBlockSize(path) /
                    1024 / 1024);
            System.out.println(fileSystem.getDefaultReplication(path));
            BlockLocation[] bls = fileSystem.getFileBlockLocations(path,
                    0, 10);
            for (BlockLocation blockLocation : bls) {
                System.out.println("HelloHdfs.hello(============================1)"
                );
                System.out.println(blockLocation.getLength());
                System.out.println(blockLocation.getOffset());
                System.out.println(Arrays.toString(blockLocation.getHosts()));
                System.out.println(Arrays.toString(blockLocation.getNames()));
                System.out.println(blockLocation.toString());
                System.out.println("HelloHdfs.hello(============================2)"
                );
            }
        }
    }

    /**
     * 文件上傳和下載
     *
     * @throws IOException
     */
    private static void upload_download() throws IOException {
//加载配置文件
        Configuration configuration = new Configuration(true);
//获取文件系统
        FileSystem fileSystem = FileSystem.get(configuration);
//文件上傳
//        Path srcPath = new Path("D:\\software\\Java反编译工具.zip");
//        Path destPath = new Path("/lzj/");
//        fileSystem.copyFromLocalFile(srcPath, destPath);


//文件下載
        Path localPath = new Path("D:\\");
        Path hdfsPath = new Path("/lzj/harry.txt");
        fileSystem.copyToLocalFile(hdfsPath, localPath);
    }
}